package com.hoyo.web;

import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hoyo.common.PageInfo;
import com.hoyo.config.UserRealm.Principal;
import com.hoyo.domain.Comment;
import com.hoyo.domain.ReportItem;
import com.hoyo.domain.User;
import com.hoyo.service.CommentService;

@Controller
public class CommentController {
	
	@Autowired
	private CommentService commentService;
	
	@RequestMapping("/comment/replyComments")
	@ResponseBody
	public String replyComments(@RequestParam("commentId") int commentId, @RequestParam("pageNum") int pageNum){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Comment> pageInfo = commentService.pageFindReplyCommentsByCommentId(commentId, pageNum);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/user/comment/new")
	@ResponseBody
	public String newComment(@RequestParam("movieId") int movieId, @RequestParam("comment") String comment,HttpSession session){
		
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Comment> pageInfo = commentService.newComment(movieId, ((User)session.getAttribute("user")).getPhone(), comment);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/user/comment/reply")
	@ResponseBody
	public String reply(@ModelAttribute() Comment comment,HttpSession session){
		JSONObject obj = new JSONObject();
		System.out.println(comment);
		try {
			PageInfo<Comment> pageInfo = commentService.newReplyComment(comment, ((User)session.getAttribute("user")));
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/comment/beReported")
	@ResponseBody
	public String beReportedComment(@RequestParam("pageNum")int pageNum, @RequestParam("pageSize") int pageSize){
		
		JSONObject obj = new JSONObject();
		try {
			PageInfo<ReportItem> pageInfo = commentService.pageFindBeReportedComments(pageNum, pageSize);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		return obj.toString();
	}
	
	@RequestMapping("/admin/comment/reportHandle")
	@ResponseBody
	public String reportHandle(@RequestParam("id")int id, @RequestParam("userId") int userId
			, @RequestParam("actionType") int actionType){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<ReportItem> pageInfo = commentService.reportHandle(id, userId, actionType);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	
	@RequestMapping("/user/comment/report")
	@ResponseBody
	public String reportComment(@RequestParam("movieId") int movieId
			, @RequestParam("commentId") int commentId, @RequestParam("reason") String reason,@RequestParam("type") String type,HttpSession session){
		JSONObject obj = new JSONObject();
		try {
			commentService.newCommentReport(movieId, commentId, ((User)(session.getAttribute("user"))).getPhone(), reason,type);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	
	@RequestMapping("/admin/comment/comments")
	@ResponseBody
	public String comments(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Comment> pageInfo = commentService.pageFindComments(pageNum, pageSize);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	
	@RequestMapping("/admin/comment/login4bid")
	@ResponseBody
	public String login4bid(@RequestParam("userId") int userId, @RequestParam("commentId") int commentId){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Comment> pageInfo = commentService.login4bid(userId, commentId);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	
	@RequestMapping("/admin/comment/comment4bid")
	@ResponseBody
	public String comment4bid(@RequestParam("userId") int userId, @RequestParam("commentId") int commentId){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Comment> pageInfo = commentService.comment4bid(userId, commentId);
			obj.put("success", true);
			obj.put("pageInfo", pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
	
	@RequestMapping("/admin/comment/remove")
	@ResponseBody
	public String remove(@RequestParam("commentId") int commentId){
		JSONObject obj = new JSONObject();
		try {
			PageInfo<Comment> pageInfo = commentService.removeOne(commentId);
			obj.put("pageInfo", pageInfo);
			obj.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			obj.put("success", false);
		}
		//TODO 消息通知
		return obj.toString();
	}
}









